package com.qiaoba.module.system.controller;

import com.qiaoba.api.system.entity.dto.UserBasicInfoDTO;
import com.qiaoba.common.base.context.UserInfoContext;
import com.qiaoba.common.base.entity.AjaxResult;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.module.system.service.SysProfileService;
import com.qiaoba.module.system.service.SysUserPostService;
import com.qiaoba.module.system.service.SysUserRoleService;
import com.qiaoba.module.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 个人信息 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/19 11:25
 */
@RestController
@RequestMapping("/system/user/profile")
@RequiredArgsConstructor
@Tag(name = "用户管理-个人中心")
public class SysProfileController {

    private final SysProfileService sysProfileService;
    private final SysUserService sysUserService;
    private final SysUserRoleService sysUserRoleService;
    private final SysUserPostService sysUserPostService;

    @GetMapping
    @Operation(summary = "基本信息")
    public AjaxResult profile() {
        String userId = UserInfoContext.getUserId();
        AjaxResult ajax = AjaxResult.success(sysUserService.selectById(userId, false));
        ajax.put("roleGroup", sysUserRoleService.selectRoleGroup(userId));
        ajax.put("postGroup", sysUserPostService.selectPostGroup(userId));
        return ajax;
    }

    @PutMapping("/update-password")
    @Operation(summary = "更改密码")
    public Result updatePassword(String oldPassword, String newPassword) {
        sysProfileService.updatePassword(oldPassword, newPassword);
        return Result.success("密码更改成功");
    }

    @PutMapping
    @Operation(summary = "更改信息")
    public Result updateBasicInfo(@RequestBody UserBasicInfoDTO dto) {
        sysProfileService.updateBasicInfo(dto);
        return Result.success("基本信息更改成功");
    }
}
